﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenCvSharp;
using System.Windows.Forms;
using System.Diagnostics;

namespace LicenseAutoDetection
{
    class ImageSearch : IDisposable
    {
        //enum ColorandGray
        //{
        //    COLOR = 3,
        //    GRAY = 1
        //}
        MainForm mainForm;
        ImageLoad LoadImg;
        ContractDetection cd;
        Morphology Mp;
        IplImage src;

        public ImageSearch(MainForm form)
        {
            mainForm = form;
            LoadImg = new ImageLoad();
            cd = new ContractDetection();
            Mp = new Morphology();
        }

        /*
         * 2차 선별
         */
        public bool Selection(string srcPath)
        {
            LoadImg.Loading(srcPath, 1, out src);

            if (src == null) // 정상적으로 이미지를 읽지 못한 경우
                return false;

            using (GrayHistogram Hist = new GrayHistogram())
            {
                return Hist.Division(src);
            }
        }

        /*
         * 3차 분석
         */
        public bool Analysis(string srcPath)
        {
            LoadImg.Loading(srcPath, 3, out src);

            if (cd.Detection(src))      // 신청서 확인
            {
                mainForm.transData.PassedTypeADD(InfoType.Contract);
                return true;
            }
            else                        // 신분증 확인
            {
                int srcSize, refSize;
                CvRect RectROI;
                IplImage processed = src.Clone();

                processed = Mp.ErodeMorphology(processed);
                processed = Mp.ErodeMorphology(processed);
                processed = Mp.DilateMorphology(processed);

                using (PixelInspection Labeling = new PixelInspection())
                {
                    RectROI = Labeling.Inspect(processed);
                    processed.Dispose();
                    processed = null;
                }

                srcSize = (int)((src.Width * src.Height) * 0.002);
                refSize = (int)((RectROI.Width + RectROI.Height) * 2);

                if (cd.Detection(src, RectROI) && srcSize > refSize)
                {
                    mainForm.transData.PassedTypeADD(InfoType.Identification);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        #region IDisposable 구현
        private bool disposed = false;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (src != null) src.Dispose();
                    if (LoadImg != null) LoadImg.Dispose();
                    if (cd != null) cd.Dispose();
                }

                disposed = true;
            }
        }

        ~ImageSearch()
        {
            Dispose(false);
        }
        #endregion
    }
}
